package com.wanmait.CarTrading.webController;

import com.wanmait.CarTrading.DAO.CarBrandDAO;
import com.wanmait.CarTrading.DAO.CarDAO;
import com.wanmait.CarTrading.DAO.CarTypeDAO;
import com.wanmait.CarTrading.DAO.impl.CarTypeDAOImpl;
import com.wanmait.CarTrading.Util.Pager;
import com.wanmait.CarTrading.vo.Brand;
import com.wanmait.CarTrading.vo.Car;
import com.wanmait.CarTrading.vo.Cartype;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/web")
public class WebCarTypeFindController
{
    @Resource
    private  CarBrandDAO carBrandDAO;
    @Resource
    private CarTypeDAO carTypeDAO;
    @Resource
private CarDAO carDAO;
    @RequestMapping("index")
     public void  index(){


     }
    @RequestMapping("info")
    public Car info(int carId){
    return carDAO.findById(carId);
    }

    @RequestMapping("list")

    public ModelAndView list(){
        ModelAndView modelAndView=new ModelAndView();
        List<Brand>brands= carBrandDAO.findAll();
        modelAndView.addObject("brands",brands);
        List<Cartype> cartypes= carTypeDAO.findAll();
        modelAndView.addObject("cartypes",cartypes);
        modelAndView.setViewName("/web/list");
        return modelAndView;

    }
    @RequestMapping("findtype")
    public ModelAndView select(int carTypeNum){
        ModelAndView mv=new ModelAndView();

        List<Car> cars=carDAO.findByCarTypeId(carTypeNum);

        mv.addObject("cars",cars);
        mv.setViewName("web/listdata");

        return mv;
    }
    @RequestMapping("listdata")
    public ModelAndView listdata(Integer carTypeId,String title,Integer pageNumber)
    {

       ModelAndView modelAndView=new ModelAndView();
      int dataCount= carDAO.getDateCount(carTypeId,title);
      if(pageNumber==null)pageNumber=pageNumber;
        Pager pager=new Pager();
        pager.setPageNum(pageNumber);
       pager.setDataCount(dataCount);
       int pagerSize=4;//从数据库读取
       pager.setPageSize(pagerSize);
    List<Car> cars= carDAO.findAll(carTypeId,title,pager);

    modelAndView.addObject("cars",cars);

    modelAndView.addObject("pager",pager);

       return modelAndView;
    }
}
